/*
Copyright 2011 Johan Maasing

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 */
package nu.zoom.catonine.swing.tail;

import java.util.List;

import javax.swing.text.DefaultStyledDocument;
import javax.swing.text.Style;
import javax.swing.text.StyleConstants;

/**
 * @author "Johan Maasing" &lt;johan@zoom.nu&gt;
 * 
 */
public class ParagraphStyleDocument extends DefaultStyledDocument {
	private static final long serialVersionUID = -7362898955384145631L;
	private ElementSpec linebreakStart;
	private ElementSpec paragraphStart;
	private final ElementSpec endSpec;
	private final static String LINE_BREAK_STYLE_NAME = "linebreakStyle";
	private final static String PARAGRAPH_BREAK_STYLE_NAME = "paragraphbreakStyle";

	public ParagraphStyleDocument() {
		this.endSpec = new ElementSpec(null, ElementSpec.EndTagType);
		this.linebreakStart = new ElementSpec(null, ElementSpec.StartTagType);
		this.paragraphStart = new ElementSpec(null, ElementSpec.StartTagType);
	}

	protected synchronized void setupStyles() {
		final Style linebreakstyle = addStyle(LINE_BREAK_STYLE_NAME, null);
		this.linebreakStart = new ElementSpec(linebreakstyle,
				ElementSpec.StartTagType);

		final Style paragraphreakstyle = addStyle(PARAGRAPH_BREAK_STYLE_NAME,
				null);
		StyleConstants.setSpaceAbove(paragraphreakstyle, 4f);
		this.paragraphStart = new ElementSpec(paragraphreakstyle,
				ElementSpec.StartTagType);
	}

	protected void addLineBreak(final List<ElementSpec> batch) {
		batch.add(this.endSpec);
		batch.add(this.linebreakStart);
	}

	protected void addParagraphBreak(final List<ElementSpec> batch) {
		batch.add(this.endSpec);
		batch.add(this.paragraphStart);
	}
}
